#/bin/bash

MODULE_NAME="test"
WORK_DIR="$(pwd)"
KERNEL_DIR="/lib/modules/$(uname -r)/extra/test_dir"
SERVICE_DIR="/usr/lib/systemd/system"
MODLOAD_DIR="/etc/modules-load.d"
MODPROBE_DIR="/etc/modprobe.d"

function install_file()
{
    install -TD -m 550 -g root -o root "$1" "$2"
}

dmesg -c > /dev/null 2>&1
clear

# make clean
# make

if [ $? -ne 0 ]; then
    echo "make error"
    exit 1
fi

install_file "${WORK_DIR}/mod_ex1.ko" "${KERNEL_DIR}/mod_ex1.ko"
install_file "${WORK_DIR}/mod_ex2.ko" "${KERNEL_DIR}/mod_ex2.ko"
# install_file "${WORK_DIR}/mod_ex3.ko" "${KERNEL_DIR}/mod_ex3.ko"
rm -rf "${KERNEL_DIR}/mod_ex3.ko"
# install_file "${WORK_DIR}/systemd-test.service" "${SERVICE_DIR}/systemd-test.service"
install_file "${WORK_DIR}/modprobe-test.conf" "${MODPROBE_DIR}/modprobe-test.conf"
# install_file "${WORK_DIR}/modprobe-ex2.conf" "${MODPROBE_DIR}/modprobe-ex2.conf"
install_file "${WORK_DIR}/modules-load-test.conf" "${MODLOAD_DIR}/modules-load-test.conf"

rm -rf "${MODPROBE_DIR}/modprobe-ex2.conf"
rm -rf "${SERVICE_DIR}/systemd-test.service"

# systemctl enable systemd-test.service
# systemctl daemon-reload

depmod -a
# modinfo test
# modprobe test
# modprobe -r test

# dmesg
echo > /var/log/messages
